package com.zxcl.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxcl.domain.Category;
import com.zxcl.domain.Prod;
import com.zxcl.domain.ProdProp;
import com.zxcl.domain.Sku;
import com.zxcl.dto.*;
import com.zxcl.model.ChangeStock;
import com.zxcl.model.Result;
import com.zxcl.service.impl.ProdService;
import com.zxcl.service.impl.SkuService;
import com.zxcl.vo.ProdSkuVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@Api(tags = "产品管理接口")
@RequestMapping("prod/prod")
public class ProdProdController  {

    @Autowired
    private ProdService prodService;

    @Autowired
    private SkuService skuService;

    /**
     * 查询所有分类信息
     * id查询也存缓存
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询所有产品信息")
    @PreAuthorize("hasAuthority('prod:prod:page')")
    public Result<Page<Prod>> loadProdPage(PageDTO pageDTO, ProdQueryDTO prodQueryDTO) {
        Page<Prod> prods = prodService.loadProdPage(pageDTO,prodQueryDTO);
        return Result.success(prods);
    }

    @GetMapping("{userId}")
    @ApiOperation("获取当前产品信息")
    @PreAuthorize("hasAuthority('prod:prod:info')")
    public Result<Prod> loadMenu(@PathVariable Long userId){
        Prod prodProp = prodService.getById(userId);
        return Result.success(prodProp);
    }

    @PostMapping
    @ApiOperation("新增商品")
    @PreAuthorize("hasAuthority('prod:prod:save')")
    public Result<String> addProd(@RequestBody @Validated ProdAddDTO prodAddDTO) {
        Integer i = prodService.addProd(prodAddDTO);
        return Result.handle(i > 0);
    }


    @PutMapping
    @ApiOperation("修改商品")
    @PreAuthorize("hasAuthority('prod:prod:update')")
    public Result<String> update(@RequestBody @Validated ProdAddDTO prodAddDTO){
        Integer i = prodService.updateProd(prodAddDTO);
        return null;
    }

    @GetMapping("getProdById")
    @ApiOperation("根据商品id查询商品信息")
    Result<Prod> getProdById(@RequestParam("prodId") Long prodId) {
        Prod prod = prodService.getById(prodId);
        return Result.success(prod);
    }

    @GetMapping("prod/prodInfo")
    @ApiOperation("根据商品id查询商品和sku集合信息")
    public Result<ProdSkuVO> getProdAndSkus(Long prodId) {
        ProdSkuVO prodSkuVO = prodService.getProdAndSkus(prodId);
        return Result.success(prodSkuVO);
    }


    @GetMapping("getSkusByIds")
    @ApiOperation("根据skuIds查询sku集合")
    Result<List<Sku>> getSkusByIds(@RequestParam("skuIds") List<Long> skuIds) {
        List<Sku> skuList = skuService.listByIds(skuIds);
        return Result.success(skuList);
    }


    /**
     * 修改库存
     * @param changeStock
     * @return
     */
    @ApiOperation("修改库存")
    @PostMapping("changeStocks")
    Result<Boolean> changeStocks(@RequestBody ChangeStock changeStock){
        Boolean flag= this.prodService.changeStocks(changeStock);
        return Result.success(flag);
    }

}
